﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace SilverApp.ViewModel
{
    public class FriendsViewModel : ViewModelBase
    {
        private List<LiveService.User>  friendsList;
        private ObservableCollection<FriendItemViewModel> friendsCollection;
        private HomeViewModel mparent;

        public FriendsViewModel(HomeViewModel parent, LiveService.User currentUser)
        {
            try
            {
                mparent = parent;
                this.initUserFriends(currentUser);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public FriendsViewModel(LiveService.User currentUser)
        {
            try 
            {              
                this.initUserFriends(currentUser);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void initUserFriends(LiveService.User currentUser)
        {
            try
            {
                this.Sclient.getUserFriendsCompleted += new EventHandler<LiveService.getUserFriendsCompletedEventArgs>(initUserFriendsCompleted);
                this.Sclient.getUserFriendsAsync(currentUser.id);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void initUserFriendsCompleted(object sender, LiveService.getUserFriendsCompletedEventArgs e)
        {
            try
            {
                this.friendsList = e.Result;
                ObservableCollection<FriendItemViewModel> tmpcol = new ObservableCollection<FriendItemViewModel>();
                foreach (LiveService.User xmp in this.friendsList)
                {
                    tmpcol.Add(new FriendItemViewModel(xmp, this.mparent));
                }
                this.FriendsCollection = tmpcol;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public ObservableCollection<FriendItemViewModel> FriendsCollection
        {
            get { return friendsCollection; }
            set
            {
                if (friendsCollection != value)
                {
                    friendsCollection = value;
                    OnPropertyChanged("FriendsCollection");
                }
            }
        }
    }
}
